<?php
namespace app\mp\admin;

use app\system\admin\Admin;
use app\common\service\Material as MaterialService;
use app\common\service\GeneralMaterial as GeneralMaterialService;

/**
 * 通用料管理
 * @author King
 *
 */
class GeneralMaterial extends Admin
{

	protected $hisiModel = '';//模型名称[通用添加、修改专用]
	protected $hisiValidate = '';
	protected $hisiAddScene = '';//添加数据验证场景名
	protected $hisiEditScene = '';//更新数据验证场景名

    public function initialize() {
    	parent::initialize();
    }

    public function index()
    {
    	if ($this->request->isAjax()) {
    		$where      = $data = [];
    		$page       = $this->request->param('page/d', 1);
    		$limit      = $this->request->param('limit/d', 20);
    		$materialName    = $this->request->param('material_name/s');
    		$materialSku    = $this->request->param('material_sku/s');

    		if ($materialName != '') {
    			$where[] = ['name', 'like', "%{$materialName}%"];
    		}
    		if ($materialSku != '') {
    			$where[] = ['id', 'in', db('general_material_sku')->where('sku', '=', $materialSku)->column('general_material_id')];
    		}

    		$order = 'id desc';
    		$data['data'] = db('general_material')->where($where)->page($page)->limit($limit)->order($order)->select();
    		$materialService = new MaterialService();
    		foreach ($data['data'] as $k => $v) {
    			$data['data'][$k]['material_skus'] = db('general_material_sku')->where('general_material_id', '=', $v['id'])->column('sku');
    		}

    		$data['count'] = db('general_material')->where($where)->count('id');
    		$data['code'] = 0;
    		$data['msg'] = '';
    		return json($data);
    	}

    	$assign = [];
    	return $this->assign($assign)->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
    	if ($this->request->isPost()) {

    		$postData = $this->request->post();

    		$generalMaterialService = new GeneralMaterialService();
    		if ($generalMaterialService->checkNameExist($postData['name'])) {
    			return $this->error('该物料名称已存在');
    		}
    		$materialSku = arrayGet($postData, 'sku', '');
    		if ($materialSku != '' && $generalMaterialService->checkSkuExist($materialSku)) {
    			return $this->error('该物料SKU已存在');
    		}

    		$postData['create_time'] = $postData['update_time'] = time();
    		unset($postData['sku']);
    		$id = db('general_material')->insertGetId($postData);
    		if (!$id) {
    			return $this->error('保存失败');
    		}
    		if ($materialSku != '') {
    			$insData = [
					'general_material_id' => $id,
    				'sku' => trim($materialSku),
    				'create_time' => time(),
    				'update_time' => time(),
    			];
    			db('general_material_sku')->insert($insData);
    		}
    		return $this->success('保存成功', '');
    	}

    	$template = $this->request->param('template', 'form');
    	return $this->fetch($template);
    }

    /**
     * 编辑
     *
     */
    public function edit()
    {
    	$id = $this->request->param('id/d', 0);
    	if ($this->request->isPost()) {// 数据验证

    		$postData = $this->request->post();                    
    		$generalMaterialService = new GeneralMaterialService();
    		if ($generalMaterialService->checkNameExist($postData['name'], $id)) {
    			return $this->error('该物料名称已存在');
    		}
    		$materialSku = arrayGet($postData, 'sku', '');
    		if ($materialSku != '' && $generalMaterialService->checkSkuExist($materialSku)) {
    			return $this->error('该物料SKU已存在');
    		}

    		$postData['update_time'] = time();
    		unset($postData['sku']);
    		db('general_material')->where('id', $id)->update($postData);
    		if ($materialSku != '') {
    			$insData = [
    				'general_material_id' => $id,
    				'sku' => trim($materialSku),
    				'create_time' => time(),
    				'update_time' => time(),
    			];
    			db('general_material_sku')->insert($insData);
    		}
    		return $this->success('保存成功', '');
    	}

    	$formData = db('general_material')->where('id', $id)->findOrEmpty();
    	$this->assign('formData', $formData);
    	$template = $this->request->param('template', 'form');

    	return $this->fetch($template);
    }

    /**
     * 删除
     *
     * @author KING
     * @date 2020年7月9日
     *
     * @return void|string
     */
    public function del()
    {
    	$id = $this->request->param('id/a');
    	db('general_material')->where('id', 'in', $id)->delete();
    	db('general_material_sku')->where('general_material_id', 'in', $id)->delete();
    	return $this->success('删除成功', '');
    }
}